@extends('layout.layer')

@section('layout.content')
  <div class="row">
    <div class="col-xs-12">
      <form class="form-horizontal" id="train-create" autocomplete="off">
        
        <div class="form-group">
          <label class="col-xs-12 control-label no-padding-right" for="type"> <b><span class="red align-middle">*</span> 培训类型：</b>
          </label>
          <div class="col-xs-12">
            <div class="radio">
              @foreach($typies as $type)
              <label class="inline no-padding-left radio-label">
                <input name="type" type="checkbox" value="{{$type->id}}" class="ace ace-checkbox-2 input-lg">
                <span class="lbl"> {{$type->name}}</span>
              </label>
              @endforeach
            </div>
          </div>
        </div>
        <div class="form-group">
          <label class="col-xs-12 control-label no-padding-right" for="branch"> <b><span class="red align-middle">*</span> 所属分支：</b>
          </label>
          <div class="col-xs-12">
            <div class="radio">
              @foreach($branchs as $branch)
              <label class="inline no-padding-left radio-label">
                <input name="branch" type="checkbox" value="{{$branch->id}}" class="ace ace-checkbox-2 input-lg">
                <span class="lbl"> {{$branch->name}}</span>
              </label>
              @endforeach           
            </div>
          </div>
        </div>

        <div class="form-group">
          <label class="col-xs-12 control-label no-padding-right" for="name"> <b><span class="red align-middle">*</span> 计划名称：</b>
          </label>
          <div class="col-xs-12">
            <input type="text" class="form-control" name="name" placeholder="计划名称" value=""/>
          </div>
        </div>
        <div class="space-32"></div>
        <div class="form-group">
          <div class="col-xs-12 center">
            <button type="submit" class="btn btn-primary" style="margin-right:15px;">
              <i class="ace-icon fa fa-check"></i>
              确定
            </button>

            <button type="button" class="btn btn-light">
              <i class="ace-icon fa fa-times"></i>
              取消
            </button>
          </div>
        </div>

      </form>
    </div>
  </div>
@stop

@section('javascript')

  <script src="//cdn.bootcss.com/jquery-validate/1.19.1/jquery.validate.min.js"></script>
  <script src="{{URL::asset('assets/js/jquery-validate-methods.js')}}"></script>
  <script src="//cdn.bootcss.com/jqueryui/1.12.1/jquery-ui.min.js"></script>


  <script type="text/javascript">
      $(function () {

          $("input:checkbox[name='type']").on('click', function (e) {
              $("input:checkbox[name='type']").not(this).attr("checked", false);
          });

          $("input:checkbox[name='branch']").on('click', function (e) {
              $("input:checkbox[name='branch']").not(this).attr("checked", false);
          });

          $('.btn-light').on('click', function(e) {  
            //parent.location.reload();
            //var index = parent.layer.getFrameIndex(window.name); 
            parent.layer.close(parent.layer.index);            
          });
          /**
           表单前端验证
           */
          $("#train-create").validate({
              rules: {                  
                  type: {required: true},
                  branch: {required: true},
                  name: {required: true},
              },
              messages: {
                  type: {required: "请选择培训类型"},
                  branch: {required: "请选择分支机构"},
                  name: {required: "请填写培训计划名称"},
              },


              /* 失去焦点时不验证 */
              onfocusout: false,
              onkeyup: false,
              errorElement: 'div',
              errorClass: 'help-block',

              success: function (e) {
                  $(e).closest('.form-group').removeClass('has-error');//.addClass('has-info');
                  $(e).remove();
              },

              showErrors: function (errorMap, errorList) {
                  $.each(errorList, function (i, v) {
                      layer.tips(v.message, v.element, {tips: [3,'#dd0000'], time: 2000});
                      return false;
                  });

              },

              submitHandler: function (form) {
                  $.post('{{route('train.store')}}', {
                      '_token': $("input[name='_token']").attr('value'),
                      'type': $("input[name='type']:checked").val(),
                      'branch': $("input[name='branch']:checked").val(),
                      'name': $("input[name='name']").val(),
                      'is_submit': true
                  }, function (data) {
                      console.log(data);
                      layer.msg(data.message, {time: 2000,shift: -1}, function () {
                          if (data.status === true) {
                              parent.location.reload();
                              parent.layer.close(parent.layer.index);
                          }
                      });

                  }, 'json').error(function (data) {
                      layer.msg(data.responseJSON.message);
                  });
              }


          });

      });
  </script>
@stop